package battleship.cxbr;

import battleship.emss.*;

import java.util.ArrayList;
import java.util.Iterator;


public class PatrolContext extends Context
{
	public int x = 0, y = 0;
	public int count = 2;
	boolean deadSub = false;
	double lastSeen = 0.0;
	Status carrier = null;
	Status nearEnemy = null;
	double nearDistance = 999999;
	boolean message = false;
	
	public PatrolContext(String n, CxBRAgent a)
	{
		super(n,a);
	}
	
	//How to get here from above
	public double ParentRule(double time)
	{
		for(int i=0;i<Agent.detectedStatus.size();i++){
			Status tmpStatus=Agent.detectedStatus.get(i);
			
			if(tmpStatus.team != Agent.status.team && 
					tmpStatus.type == Agent.SUB &&
					tmpStatus.alive == 1){
				lastSeen = time;
			}
		}
		if (time - lastSeen > 3000){
			return 0.9;
		}
		return 0.0;
	}
	//How to get here from side
	public double SiblingRule(double time)
	{
		//if (x++ % 1000 == 0)
		//	System.out.println("Search Sibling Rule");
		//count = ++count % 5;
		for(int i=0;i<Agent.detectedStatus.size();i++)
		{
			Status tmpStatus=Agent.detectedStatus.get(i);
			
			if(	tmpStatus.team != Agent.status.team && 
				tmpStatus.type == Agent.SUB &&
				tmpStatus.alive == 0){
				deadSub = true;
				return 1.0d;
			}
		}
		return 0.0d;
	}
	
	//Things to do when just entering
	public void Initialize(double time)
	{
		lastSeen = time;
	}

	
	//What do actually do
	public void Action(double time){
		double speed = 3.0d;

		//Agent.status.status = "Protect Carrier";
		Agent.actions.clear();
		if (Agent.detectedStatus.size() > 0){
			Evaluate.updateSightings(Agent.detectedStatus);
		}
		
		for(int i = 0; i < Agent.detectedStatus.size(); i++){
			Status tmpStatus = Agent.detectedStatus.get(i);
			if (tmpStatus.team != Agent.status.team &&
						tmpStatus.alive == 1){
				if (nearEnemy == null || nearEnemy.alive == 0){
					nearEnemy = tmpStatus;
				} else if (Evaluate.fleetCarrier != null && Evaluate.distance(Evaluate.fleetCarrier, tmpStatus) < nearDistance){
					nearEnemy = tmpStatus;
					nearDistance = Evaluate.distance(Evaluate.fleetCarrier, tmpStatus);
				}
			}
		}
		
		if (Evaluate.enemyLeft.size() > 0 && !message){
			Iterator enemyLeftIter = Evaluate.enemyLeft.keySet().iterator();
			while (enemyLeftIter.hasNext()){
				String key = (String) enemyLeftIter.next();
				if (!Evaluate.enemyLeft.get(key)){
					Agent.outgoingMessages.add(new Message("Command", "Sub Leaving Port"));
					Evaluate.enemyLeft.put(key, Boolean.TRUE);
					message = true;
				}
			}
		}
		if (nearEnemy != null && Evaluate.fleetCarrier != null){
			String targetName = Evaluate.highestThreat(Agent.status);
			
			//System.out.println("Near is: " + nearEnemy.name);
			double dist = Evaluate.distance(Agent.status.mx, Agent.status.my, Evaluate.targetX, Evaluate.targetY);
			if (message && Agent.outgoingMessages.size() == 0){
				System.out.println("Message sent");
				message = false;
			}
			if (!message && Agent.outgoingMessages.size() == 0){
				speed = Math.max(1, Math.min(13, (dist / 3000)));
			}
			//System.out.println("(" + Evaluate.targetX + ", " + Evaluate.targetY + ")");
			Agent.status.status = " Guard: " + targetName;
			Agent.actions.add(new Move(
							Agent.status.mx, 
							Agent.status.my, 
							Evaluate.targetX,
							Evaluate.targetY,
							speed));
		}
		return;
	}
	
}

